<template>
  <page-header-wrapper>
    <a-page-header style="background-color: #fff" sub-title="可提现余额">
      <div style="display: flex">
        <h2>￥{{ share_profit || '0.00' }}</h2>
        <a-button type="primary" style="margin-left: 20px; align-items: center" @click="showWithdraw = true"
          >提现</a-button
        >
      </div>
    </a-page-header>
    <a-card :bordered="false" class="card_style_re" style="margin-top: 16px">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getShopList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未到账 </a-radio-button>
          <a-radio-button value="1"> 已到账 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="shopList"
      >
        <span slot="account" slot-scope="scope">
          <a-popover title="收款账号">
            <template slot="content">
              <div>{{ scope.type == 0 ? '微信' : scope.type == 1 ? '支付宝' : '银行卡' }}账号:{{ scope.account }}</div>
              <div>
                {{ scope.type == 0 ? '微信昵称' : scope.type == 1 ? '支付宝昵称' : '姓名' }}:{{ scope.account_name }}
              </div>
              <div v-if="scope.type == 2">银行开户行:{{ scope.bank }}</div>
            </template>
            <div>{{ scope.type == 0 ? '微信' : scope.type == 1 ? '支付宝' : '银行卡' }}账号:{{ scope.account }}</div>
          </a-popover>
        </span>
        <span slot="amount" slot-scope="text"> ￥{{ text || '0.00' }} </span>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope.status == 0 ? 'warning' : scope.status == 1 ? 'open' : 'close'"></span>
            <span>{{ scope.status == 0 ? '待审核' : scope.status == 1 ? '已同意' : '已拒绝' }}</span>
          </div>
        </div>
      </a-table>
    </a-card>
    <!-- 提现申请弹窗 -->
    <a-modal
      title="申请提现"
      v-model="showWithdraw"
      :afterClose="closeModal"
      @ok="submitApply"
      :destroyOnClose="true"
      width="800px"
    >
      <a-form-model
        ref="ruleForm"
        :model="withdrawForm"
        :rules="rules"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-form-model-item label="提现方式" prop="type">
          <a-radio-group v-model="withdrawForm.type">
            <a-radio value="0"> 微信 </a-radio>
            <a-radio value="1"> 支付宝 </a-radio>
            <a-radio value="2"> 银行卡 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item
          :label="withdrawForm.type == 0 ? '微信号' : withdrawForm.type == 1 ? '支付宝账号' : '银行卡号'"
          prop="account"
        >
          <a-input
            v-model="withdrawForm.account"
            :placeholder="`请输入${
              withdrawForm.type == 0 ? '微信号' : withdrawForm.type == 1 ? '支付宝账号' : '银行卡号'
            }`"
          />
        </a-form-model-item>
        <a-form-model-item label="银行卡开户行" prop="bank" v-if="withdrawForm.type == 2">
          <a-input v-model="withdrawForm.bank" placeholder="请输入银行开户行" />
        </a-form-model-item>
        <a-form-model-item
          :label="withdrawForm.type == 0 ? '微信昵称' : withdrawForm.type == 1 ? '支付宝昵称' : '姓名'"
          prop="account_name"
        >
          <a-input
            v-model="withdrawForm.account_name"
            :placeholder="`请输入${
              withdrawForm.type == 0 ? '微信昵称' : withdrawForm.type == 1 ? '支付宝昵称' : '姓名'
            }`"
          />
        </a-form-model-item>
        <a-form-model-item label="提现金额" prop="amount">
          <a-input v-model="withdrawForm.amount" placeholder="请输入提现金额" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { withdrawList, createApply } from '@/api/saas/agency'
const columns = [
  {
    title: '提现账号',
    scopedSlots: {
      customRender: 'account',
    },
  },
  {
    title: '提现金额',
    dataIndex: 'amount',
    scopedSlots: {
      customRender: 'amount',
    },
    sorter: (a, b) => {
      a.amount - b.amount
    },
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '申请时间',
    dataIndex: 'created_at',
  },
]

export default {
  data() {
    let checkAccount = (rule, value, callback) => {
      console.log(rule, value)
      if (!value) {
        if (rule.field == 'account') {
          if (this.withdrawForm.type == 0) {
            callback(new Error('请输入微信号'))
          }
          if (this.withdrawForm.type == 1) {
            callback(new Error('请输入支付宝账号'))
          }
          if (this.withdrawForm.type == 2) {
            callback(new Error('请输入银行卡卡号'))
          }
        }
        if (rule.field == 'name') {
          if (this.withdrawForm.type == 0) {
            callback(new Error('请输入微信昵称'))
          }
          if (this.withdrawForm.type == 1) {
            callback(new Error('请输入支付宝昵称'))
          }
          if (this.withdrawForm.type == 2) {
            callback(new Error('请输入姓名'))
          }
        }
      } else {
        callback()
      }
    }
    return {
      // 查询参数
      queryParam: {
        name: '',
        mobile: '',
        type: '',
        status: '-1',
        pageNo: 1,
        pageSize: 10,
        is_daili:1
      },
      // 已选中行的id
      selectedRowKeys: [],
      // 门店列表
      shopList: [],
      columns,
      // 数据总条数
      totalCount: 0,
      // 是否显示申请提现的弹窗
      showWithdraw: false,
      btnLoading: false,
      // 提现表单
      withdrawForm: {
        amount: '',
        type: '0', // 0微信  1支付宝  2银行卡
        account_name: '',
        account: '',
        bank: '',
      },
      rules: {
        amount: [{ required: true, message: '请输入提现金额', trigger: 'blur' }],
        account: [{ required: true, validator: checkAccount, trigger: 'blur' }],
        account_name: [{ required: true, validator: checkAccount, trigger: 'blur' }],
        bank: [{ required: true, message: '请输入银行卡开户行', trigger: 'blur' }],
      },
      // 可提现余额
      share_profit: 0,
    }
  },
  methods: {
    // 关闭弹窗的回调
    closeModal() {
      this.$refs['ruleForm'].resetFields()
    },
    // 获取进件列表
    async getShopList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await withdrawList(this.queryParam)
      console.log(res)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.shopList = res.data.data
        this.share_profit = res.data.share_profit
      }
      hide()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getShopList()
    },
    // 创建提现申请
    submitApply() {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          this.btnLoading = true
          const res = await createApply(this.withdrawForm)
          if (res.code == 0) {
            this.showWithdraw = false
            this.getShopList()
          }
          this.btnLoading = false
        } else {
          return false
        }
      })
    },
  },
  created() {
    this.getShopList()
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  padding: 10px 0;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #ff4400;
  }
  .open {
    background-color: #65c817;
  }
  .warning {
    background-color: #eeb041;
  }
}
</style>